import cgi
import wsgiref.handlers
import os

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template

class QW_GsScore(db.Model):
    id = db.StringProperty()
    score = db.IntegerProperty()
    snsFlag = db.IntegerProperty();
    date = db.DateTimeProperty(auto_now_add=True)
    
class QW_sns():
    snsArr = ['x','5','m','t','k']
    def getId(self, name=None):
        for i in range(len(self.snsArr)):
            if self.snsArr[i] == name:
                return i
        return -1
    def getWebSiteAbb(self, id=None):
        if id > -1 and id < len(self.snsArr):
            return self.snsArr[id]

class QW_getScore(webapp.RequestHandler):
    DEFAULT_TOP_NUM = 10
    def get(self):
        topNum = self.request.get('num')
        if topNum:
            topNum = int(topNum)
            if topNum < 1 or topNum > 1000:
                topNum = self.DEFAULT_TOP_NUM
        else:
            topNum = self.DEFAULT_TOP_NUM
        tops = QW_GsScore.all().order('-score').fetch(topNum)
        template_values = {'tops' : tops, 'topNum':topNum}
        path = os.path.join(os.path.dirname(__file__), '../template/top10.html')
        self.response.out.write(template.render(path, template_values))
    def post(self):
        self.get()

def main():
  application = webapp.WSGIApplication(
                                       [('/top10', QW_getScore)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()